package com.ruoyi.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.domain.ProjectColor;
import com.ruoyi.project.mapper.ProjectColorMapper;
import com.ruoyi.project.service.ProjectColorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ProjectColorServiceImpl extends ServiceImpl<ProjectColorMapper, ProjectColor> implements ProjectColorService {

    @Override
    public ProjectColor getProjectColor(Long userId) {
        LambdaQueryWrapper<ProjectColor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectColor::getUserId,userId);
        queryWrapper.last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void addProjectColor(ProjectColor req) {
        if(Objects.isNull(req.getId())){
            LambdaQueryWrapper<ProjectColor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProjectColor::getUserId,req.getUserId());
           List<ProjectColor> list = this.baseMapper.selectList(queryWrapper);
           if(!CollectionUtils.isEmpty(list)){
             throw new RuntimeException("用户已存在颜色配置不能重复新增");
           }
        }
        this.saveOrUpdate(req);
    }
}